

#import "DirectoryContactXMLReader.h"
#import "IDominoAppDelegate.h"
static NSUInteger parsedPersonEntriesCounter;

@implementation DirectoryContactXMLReader

@synthesize currentPersonObject = _currentPersonObject;
@synthesize currentColumnNumber=_currentColumnNumber;
@synthesize allPersonNumber = _allPersonNumber;
@synthesize contentOfCurrentPersonProperty = _contentOfCurrentPersonProperty;
@synthesize firstNameList = _firstNameList;
@synthesize lastNameList = _lastNameList;
@synthesize UNIDList = _UNIDList;
#define MAX_PERSONS 1000

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    parsedPersonEntriesCounter = 0;
}

- (NSArray*)parseXMLFileFromData:(NSData *)data parseError:(NSError **)error
{	
	self.firstNameList=[[NSMutableArray alloc] init];
	self.lastNameList=[[NSMutableArray alloc] init];
	self.UNIDList=[[NSMutableArray alloc] init];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
    
    [parser release];
	return [[[NSArray alloc]initWithObjects:self.firstNameList, self.lastNameList, self.UNIDList,nil] autorelease];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }  
    if ([elementName isEqualToString:@"viewentries"]) {
		NSNumber *topLevelEntries = [attributeDict valueForKey:@"toplevelentries"];
		self.allPersonNumber=topLevelEntries;
		return;
	}
	else if ([elementName isEqualToString:@"viewentry"]) {	
        parsedPersonEntriesCounter++;
		NSString* UNID=[attributeDict valueForKey:@"unid"];
		if(UNID==nil)
			UNID=@"";
		[self.UNIDList addObject:UNID];
    }
	
    else if ([elementName isEqualToString:@"entrydata"]) {
        self.currentColumnNumber = [attributeDict valueForKey:@"columnnumber"];
		
    } 
	else if ([elementName isEqualToString:@"text"]) {
        self.contentOfCurrentPersonProperty = [NSMutableString stringWithCapacity:30];
	} 
	else {
        self.contentOfCurrentPersonProperty = nil;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
    if (qName) {
        elementName = qName;
    }
    
    if ([elementName isEqualToString:@"text"]) {
		if(self.contentOfCurrentPersonProperty==nil || self.contentOfCurrentPersonProperty==@"")
			return;
		else if ([self.currentColumnNumber  isEqualToString:@"1"]) {
			NSString* fullName=self.contentOfCurrentPersonProperty;
			NSRange range=[fullName rangeOfString:@", "];
			if(range.location==NSNotFound){
				[self.firstNameList addObject:fullName];
				[self.lastNameList addObject:@""];
				return;
			}				
			[self.lastNameList addObject:[fullName substringToIndex:range.location]];
			[self.firstNameList addObject:[fullName substringFromIndex:range.location+range.length]];
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentPersonProperty) {
		[self.contentOfCurrentPersonProperty appendString:string];
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
    }
}

@end
